let ocrData = require('./ocr.json');
let yoloData = require('./yolores.json');
let yTotal = 232;
let xTotal = 494;
let fs = require('fs')
function distance(ax, ay, bx, by) {
    return Math.round(Math.sqrt((ax - bx) * (ax - bx) + (ay - by) * (ay - by)))
}

const _Thres = 0.12
const _MaxDIS = Math.round(distance(0, 0, xTotal, yTotal) * _Thres)

function iscross(ax, ay, bx, by, aw, ah, bw, bh) {
    // 横向和纵向距离
    let verti_dis = Math.abs(ax - bx);
    let horiz_dis = Math.abs(ay - by);
    // 高度和宽度之和的一半
    let half_w = (aw + bw) / 2;
    let half_y = (ah + bh) / 2;
    if (verti_dis < half_y && horiz_dis < half_w) {
        return true;
    }
    return false;
}

function sliceByCol(words) {
    let splited = words.trim().split(":")
    if (splited.length == 2) {
        if (splited[1].trim() != '') {
            return splited
        } else {
            return splited.splice(1, 1)
        }
    }
    return splited
}

function sliceBySpace(words) {
    return words.trim().split(/\s+/)
}

(
    async () => {
        let yoloPos = []
        let textPos = []
        let boxPos = yoloData.position
        for await (const line of boxPos) {
            let typeBox = line[0]
            let x_center = +line[1] * xTotal
            let y_center = +line[2] * yTotal
            let x_width = +line[3] * xTotal
            let y_width = +line[4] * yTotal
            // console.log(typeBox, Math.round(x_center - x_width / 2), Math.round(y_center - y_width / 2), Math.round(x_center + x_width / 2), Math.round(y_center + y_width / 2))
            // console.log(typeBox, Math.round(x_center), Math.round(y_center))
            yoloPos.push({
                x_center: Math.round(x_center),
                y_center: Math.round(y_center),
                x_width: Math.round(x_width),
                y_width: Math.round(y_width),
                predict: typeBox,
                before_text: "",
                inner_text: []
            })
        }
        let ocrele = ocrData.words_result
        for await (const line of ocrele) {
            let bbox = line.location
            let x_width = +bbox.width
            let y_width = +bbox.height
            // console.log(line.words, bbox.left, bbox.top, bbox.left + x_width, bbox.top + y_width);
            // console.log(line.words, Math.round(bbox.left + x_width / 2), Math.round(bbox.top + y_width / 2));
            textPos.push({
                x_center: Math.round(bbox.left + x_width / 2),
                y_center: Math.round(bbox.top + y_width / 2),
                x_width: x_width,
                y_width: y_width,
                text: line.words
            })
        }
        for (let i = 0; i < yoloPos.length; i++) {
            // console.log("----", i, "-----")
            for (let j = 0; j < textPos.length; j++) {
                if (iscross(yoloPos[i].x_center, yoloPos[i].y_center, textPos[j].x_center, textPos[j].y_center,
                    yoloPos[i].y_width, yoloPos[i].x_width, textPos[j].y_width, textPos[j].x_width)) {
                    // console.log(yoloPos[i].type, textPos[j].text)
                    let splicRes = sliceByCol(textPos[j].text)
                    if (splicRes.length == 2) {
                        yoloPos[i].before_text += splicRes[0]
                        yoloPos[i].inner_text.push(sliceBySpace(splicRes[1]))
                    } else {
                        yoloPos[i].inner_text.push(sliceBySpace(textPos[j].text))
                    }
                    // console.log(yoloPos[i])

                    textPos.splice(j, 1)
                    continue;
                }
                if (distance(yoloPos[i].x_center, yoloPos[i].y_center, textPos[j].x_center, textPos[j].y_center) < _MaxDIS) {
                    // console.log(yoloPos[i].type, textPos[j].text)
                    yoloPos[i].before_text += textPos[j].text
                    // console.log(yoloPos[i])

                }
            }
        }
        let resJson = []
        for (let i = 0; i < yoloPos.length; i++) {
            resJson.push({
                position: [
                    [Math.round(yoloPos[i].x_center - yoloPos[i].x_width / 2), Math.round(yoloPos[i].y_center + yoloPos[i].y_width / 2)],
                    [Math.round(yoloPos[i].x_center + yoloPos[i].x_width / 2), Math.round(yoloPos[i].y_center + yoloPos[i].y_width / 2)],
                    [Math.round(yoloPos[i].x_center + yoloPos[i].x_width / 2), Math.round(yoloPos[i].y_center - yoloPos[i].y_width / 2)],
                    [Math.round(yoloPos[i].x_center - yoloPos[i].x_width / 2), Math.round(yoloPos[i].y_center - yoloPos[i].y_width / 2)],
                ],

                "predict": yoloPos[i].predict,
                "before_text": yoloPos[i].before_text,
                "inner_text": yoloPos[i].inner_text[0] || [],
            })
        }

        fs.writeFileSync('./example.json', JSON.stringify(resJson))
    }
)()
